<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>throttle demo</title>
    <script>
        function coloring() {
            let r = Math.floor(Math.random() * 255)
            let g = Math.floor(Math.random() * 255)
            let b = Math.floor(Math.random() * 255)
            document.body.style.background = `rgb(${r},${g},${b})`;
        }

        function throttle(func, delay) {
            let timer
            return function () {
                if (timer) {
                    return
                }
                timer = setTimeout(function () {
                    func.apply(this, arguments)
                    timer = null
                }, delay)

            }
        }
        window.addEventListener('resize', throttle(coloring, 2000))
    </script>
</head>

<body>
</body>

</html>